from output import Output
from plugin_system import Plugin
from parameter_system import Enum

import copy

class PlanetParametersOutput(Output):
    def __init__(self, filename=None):
        self.filename = filename
        self.fl = None

    def initialize(self):
        """
        Opens the output file.
        """
        self.fl = open(self.filename, "w")

    def finalize(self):
        """
        Closes the output file.
        """
        # only write out parameters at end
        for name in self.planet_names:
            ln = "%s %s\n" % (name, self.masses[name])
            self.fl.write(ln)
        self.fl.close()

    def output(self, system, step):
        self.masses = copy.deepcopy(system.masses)
        self.planet_names = copy.deepcopy(system.planet_names)
            

class PlanetParametersOutputPluginAdapter(Plugin, PlanetParametersOutput):
    plugin_name = "PlanetParametersOutput"
    parameter_definitions = {"filename" : (str, True, "File to which to write data.")}

    def __init__(self, *args, **user_params):
        params = self.configure_parameters(**user_params)
        PlanetParametersOutput.__init__(self, filename=params["filename"])
